package com.android.multiplesmart.api;

import android.util.Log;

import com.android.multiplesmart.bean.LaguageAllBean;
import com.schillerchina.baselibrary.utils.GsonUtil;
import com.schillerchina.baselibrary.utils.ToastUtil;

import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BaseHttp{

    private String url;
    private Map<String,Object> map;
    private static  BaseHttp baseHttp;
    private OnCallBack onCallBack;
    private int state;

    public  static  BaseHttp getBaseHttp(){
         if (baseHttp==null){
              synchronized (BaseHttp.class){
                   if (baseHttp==null){
                       baseHttp=new BaseHttp();
                   }
              }
         }
         return   baseHttp;
    }

    public String getUrl() {
        return url;
    }

    public BaseHttp setUrl(String url) {
        this.url = url;
        return  this;
    }

    public Map<String, Object> getMap() {
        return map;
    }

    public BaseHttp setMap(Map<String, Object> map) {
        this.map = map;
        return  this;
    }

    public OnCallBack getOnCallBack() {
        return onCallBack;
    }

    public BaseHttp setOnCallBack(OnCallBack onCallBack) {
        this.onCallBack = onCallBack;
        return  this;
    }

    public int getState() {
        return state;
    }

    public BaseHttp setState(int state) {
        this.state = state;
        return  this;
    }

    public void   request(Api api){
        System.out.println("下是啊s");
             api.getAllLanguageName(url,map).enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    if(GsonUtil.getCodes(response.body())==0){
                        if (onCallBack!=null){
                              onCallBack.onSusses(call,response.body(),state);

                        }
                    }else {
                        ToastUtil.showToast(GsonUtil.getInfo(response.body()));
                    }
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    if (onCallBack!=null){
                        onCallBack.onError(call,t,state);
                    }
                }
            });
    }

    public void   requests(Api api){
        System.out.println("下是啊s");
        api.getMic(url).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(GsonUtil.getCodes(response.body())==0){
                    if (onCallBack!=null){
                        onCallBack.onSusses(call,response.body(),state);
                    }
                }else {
                    ToastUtil.showToast(GsonUtil.getInfo(response.body()));
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                if (onCallBack!=null){
                    onCallBack.onError(call,t,state);
                }
            }
        });
    }



    public  interface  OnCallBack{
        void  onSusses(Call<String> call,String value,int state);
        void  onError(Call<String> cal,Throwable t,int state);
    }
}
